package us.nchu.tomtiger.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.enums.ResultEnum;
import us.nchu.tomtiger.model.Result;
import us.nchu.tomtiger.model.SmsVerifyCode;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.model.UserShow;
import us.nchu.tomtiger.service.SmsVerifyCodeService;
import us.nchu.tomtiger.service.UserService;
import us.nchu.tomtiger.service.UserShowService;
import us.nchu.tomtiger.utils.ResultUtil;
import us.nchu.tomtiger.utils.UserUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by XiongXuan on 2018/1/2.
 */
@RequestMapping("/register")
@Controller
public class RegisterController {
    @Resource
    private SmsVerifyCodeService smsVerifyCodeService;
    @Resource
    private UserService userService;
    @Resource
    private UserShowService userShowService;

    @GetMapping
    public ModelAndView index() {
        return new ModelAndView("register");
    }

    @PostMapping
    @ResponseBody
    @Transactional
    public Result register(HttpServletRequest httpServletRequest) throws UnsupportedEncodingException {
        // 获取传入的注册参数
        String phoneNumber = httpServletRequest.getParameter("phoneNumber");
        String verifyCode = httpServletRequest.getParameter("verifyCode");
        String passwd = httpServletRequest.getParameter("password");
        String nickName = new String(httpServletRequest.getParameter("nickName").getBytes("UTF-8"));

        // 通过手机号码，查询出发送验证码的记录
        SmsVerifyCode smsVerifyCode = smsVerifyCodeService.query(phoneNumber);
        if (smsVerifyCode == null) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_REGISTER_NO_SMS);
        }

        // 验证码是否正确
        if (!verifyCode.equals(smsVerifyCode.getCode())) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_REGISTER_WRONG_SMS_CODE);
        }

        // 验证码是否超时
        System.out.println("time = " + smsVerifyCode.getTime());
        if (UserUtil.checkTime(smsVerifyCode.getTime().getTime())) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_REGISTER_SMS_TIME_LIMIT);
        }

        // 插入数据库
        try {
            userService.createUser(new User(phoneNumber, passwd, nickName));
            userShowService.createShow(new UserShow(phoneNumber));
        }catch (DuplicateKeyException e) {
            // 已经注册过了
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_REGISTER_DUPLICATE_PHONE_NUMBER);
        }

        // 将注册信息写入登录状态
        UserUtil.writeUserInfo(httpServletRequest, new User(phoneNumber, passwd, nickName));

        httpServletRequest.getSession().setAttribute("firstRegister", "true");

        return ResultUtil.success("ok");
    }
}
